package com.hp.beanstalk;

import com.dinstone.beanstalkc.BeanstalkClientFactory;
import com.dinstone.beanstalkc.JobConsumer;
import com.dinstone.beanstalkc.JobProducer;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Bird
 */
@Configuration
public class BeanstalkConfig {

    @Resource
    private BeanStalkProperties properties;

    /**
     * 创建一个BeanstalkClientFactory
     */
    @Bean
    public BeanstalkClientFactory getFactory() {
        com.dinstone.beanstalkc.Configuration config = new com.dinstone.beanstalkc.Configuration();
        config.setServiceHost(properties.getHost());
        config.setServicePort(properties.getPort());
        return new BeanstalkClientFactory(config);
    }

    /**
     * 创建一个生产者
     */
    @Bean
    public JobProducer jobProducer() {
        return getFactory().createJobProducer(properties.getTube());
    }

    /**
     * 创建一个消费者
     */
    @Bean
    public JobConsumer jobConsumer() {
        return getFactory().createJobConsumer(properties.getTube());
    }

}
